<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Patch a string with an unified diff</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.xdiff-string-patch-binary.html">« xdiff_string_patch_binary</a></li>
      <li style="float: right;"><a href="function.xdiff-string-rabdiff.html">xdiff_string_rabdiff »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.xdiff.html">xdiff 函数</a></li>
    <li>Patch a string with an unified diff</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.xdiff-string-patch" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">xdiff_string_patch</h1>
  <p class="verinfo">(PECL xdiff &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">xdiff_string_patch</span> &mdash; <span class="dc-title">Patch a string with an unified diff</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.xdiff-string-patch-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>xdiff_string_patch</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$patch</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = ?</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter reference">&$error</code><span class="initializer"> = ?</span></span><br>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   Patches a <code class="parameter">str</code> string with an unified patch in <code class="parameter">patch</code> parameter 
   and returns the result. <code class="parameter">patch</code> has to be an unified diff created by 
   <span class="function"><a href="function.xdiff-file-diff.html" class="function">xdiff_file_diff()</a></span>/<span class="function"><a href="function.xdiff-string-diff.html" class="function">xdiff_string_diff()</a></span> function. 
   An optional <code class="parameter">flags</code> parameter specifies mode of operation. Any
   rejected parts of the patch will be stored inside <code class="parameter">error</code> variable if 
   it is provided.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.xdiff-string-patch-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">str</code></dt>

     <dd>

      <p class="para">
       The original string.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">patch</code></dt>

     <dd>

      <p class="para">
       The unified patch string. It has to be created using <span class="function"><a href="function.xdiff-string-diff.html" class="function">xdiff_string_diff()</a></span>, 
       <span class="function"><a href="function.xdiff-file-diff.html" class="function">xdiff_file_diff()</a></span> functions or compatible tools.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">flags</code> can be either
       <strong><code>XDIFF_PATCH_NORMAL</code></strong> (default mode, normal patch)
       or <strong><code>XDIFF_PATCH_REVERSE</code></strong> (reversed patch).
      </p>
      <p class="para">
       Starting from version 1.5.0, you can also use binary OR to enable
       <strong><code>XDIFF_PATCH_IGNORESPACE</code></strong> flag.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">error</code></dt>

     <dd>

      <p class="para">
       If provided then rejected parts are stored inside this variable.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.xdiff-string-patch-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the patched string, or <strong><code>false</code></strong> on error.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.xdiff-string-patch-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2253">
    <p><strong>示例 #1 <span class="function"><strong>xdiff_string_patch()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     The following code applies changes to some article.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$old_article&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./old_article.txt'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$diff&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$_SERVER</span><span style="color: #007700">[</span><span style="color: #DD0000">'patch'</span><span style="color: #007700">];&nbsp;</span><span style="color: #FF8000">/*&nbsp;Let's&nbsp;say&nbsp;that&nbsp;someone&nbsp;pasted&nbsp;a&nbsp;patch&nbsp;to&nbsp;html&nbsp;form&nbsp;*/<br /><br /></span><span style="color: #0000BB">$errors&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$new_article&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">xdiff_string_patch</span><span style="color: #007700">(</span><span style="color: #0000BB">$old_article</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$diff</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">XDIFF_PATCH_NORMAL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$errors</span><span style="color: #007700">);<br />if&nbsp;(</span><span style="color: #0000BB">is_string</span><span style="color: #007700">(</span><span style="color: #0000BB">$new_article</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"New&nbsp;article:\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$new_article</span><span style="color: #007700">;<br />}<br /><br />if&nbsp;(</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$errors</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Rejects:&nbsp;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$errors</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.xdiff-string-patch-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.xdiff-string-diff.html" class="function" rel="rdfs-seeAlso">xdiff_string_diff()</a> - Make unified diff of two strings</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>